package com.jiunuan.live.api.service.impl;

import com.jiunuan.live.api.service.ILivingRoomService;
import com.jiunuan.live.api.vo.LivingRoomInitVO;
import com.jiunuan.live.api.vo.req.LivingRoomReqVO;
import com.jiunuan.live.api.vo.resp.LivingRoomPageRespVO;
import com.jiunuan.live.api.vo.resp.LivingRoomRespVO;
import com.jiunuan.live.common.interfaces.dto.PageWrapper;
import com.jiunuan.live.common.interfaces.utils.ConvertBeanUtils;
import com.jiunuan.live.living.interfaces.dto.LivingRoomReqDTO;
import com.jiunuan.live.living.interfaces.dto.LivingRoomRespDTO;
import com.jiunuan.live.living.interfaces.rpc.ILivingRoomRpc;
import com.jiunuan.live.user.dto.UserDTO;
import com.jiunuan.live.user.interfaces.IUserRpc;
import com.jiunuan.live.web.starter.context.JiuNuanRequestContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @Author: JiuNuan
 * @CreateTime: 2024-08-19 15:11
 * @Description:
 */
@Service
public class LivingRoomServiceImpl implements ILivingRoomService {
    @DubboReference
    private ILivingRoomRpc livingRoomRpc;
    @DubboReference
    private IUserRpc userRpc;

    /**
     * 获取直播间列表
     *
     * @param livingRoomReqVO
     * @return
     */
    @Override
    public LivingRoomPageRespVO list(LivingRoomReqVO livingRoomReqVO) {
        PageWrapper<LivingRoomRespDTO>  resultPage = livingRoomRpc.list(ConvertBeanUtils.convert(livingRoomReqVO,LivingRoomReqDTO.class));
        LivingRoomPageRespVO livingRoomPageRespVO = new LivingRoomPageRespVO();
        livingRoomPageRespVO.setList(ConvertBeanUtils.convertList(resultPage.getList(), LivingRoomRespVO.class));
        livingRoomPageRespVO.setHasNext(resultPage.isHasNext());
        return livingRoomPageRespVO;
    }

    /**
     * 开启直播间
     *
     * @param type
     */
    @Override
    public Integer startingLiving(Integer type) {
        Long userId = JiuNuanRequestContext.getUserId();
        UserDTO userDTO = userRpc.getUserById(userId);
        LivingRoomReqDTO livingRoomReqDTO = new LivingRoomReqDTO();
        livingRoomReqDTO.setAnchorId(userId);
        // todo 测试直播间
        livingRoomReqDTO.setRoomName("主播-" + JiuNuanRequestContext.getUserId() + "的直播间");
        livingRoomReqDTO.setCovertImg(userDTO.getAvatar());
        livingRoomReqDTO.setType(type);
        return livingRoomRpc.startLivingRoom(livingRoomReqDTO);

    }

    /**
     * 关闭直播间
     *
     * @param roomId
     */
    @Override
    public boolean closeLiving(Integer roomId) {
        LivingRoomReqDTO livingRoomReqDTO = new LivingRoomReqDTO();
        livingRoomReqDTO.setRoomId(roomId);
        livingRoomReqDTO.setAnchorId(JiuNuanRequestContext.getUserId());
        return livingRoomRpc.closeLiving(livingRoomReqDTO);
    }

    /**
     * 获取主播相关配置信息（只有主播才有权限）
     *
     * @param userId 用户ID，用于验证用户是否为主播
     * @param roomId 房间ID，用于查询房间信息
     * @return 返回一个LivingRoomInitVO对象，包含房间初始化信息，包括是否为主播等
     */
    @Override
    public LivingRoomInitVO anchorConfig(Long userId, Integer roomId) {
        // 根据房间ID查询房间相关信息
        LivingRoomRespDTO respDTO = livingRoomRpc.queryByRoomId(roomId);
        UserDTO userDTO = userRpc.getUserById(userId);
        // 创建一个房间初始化信息对象用于返回
        LivingRoomInitVO respVO = new LivingRoomInitVO();
        respVO.setWatcherAvatar(userDTO.getNickName());
        respVO.setUserId(userId);
        // todo 给定默认用户头像
        String avatarUrl = "https://ts1.cn.mm.bing.net/th/id/R-C.9374656d321c678f308d99fd25105c0b?rik=tvQL0ModphBaTg&riu=http%3a%2f%2fsemsrc.51xiazai.cn%2fd%2ffile%2fpcsoftware%2fshprj%2f2021-01-07%2faf70130015632eecefe53aa488767bb6.jpg&ehk=7efPDFE9lV%2fbrK0ODO21U8659azJjJcY1crRiB3GsXk%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1";
        respVO.setAvatar(StringUtils.isEmpty(userDTO.getAvatar())?avatarUrl:userDTO.getAvatar());
        // 如果房间信息为空，或主播ID为空，或用户ID为空，则该用户不是主播
        if(respDTO == null || respDTO.getAnchorId() == null || userId == null){
            // 返回房间ID为-1，表示直播间不存在
            respVO.setRoomId(-1);
        }else{
            // 判断用户ID是否与房间的主播ID一致，一致则为主播，否则不是
            respVO.setRoomId(respDTO.getId());
            respVO.setAnchorId(respDTO.getAnchorId());
            respVO.setAnchor(respDTO.getAnchorId().equals(userId));
        }
        // 返回房间初始化信息对象
        return respVO;
    }
}
